package com.example.usermanagement.liuka.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.usermanagement.exception.ObjectNotException;
import com.example.usermanagement.exception.ParamErrorException;
import com.example.usermanagement.exception.ServerErrorException;
import com.example.usermanagement.liuka.mapper.FlowPackageUserMapper;
import com.example.usermanagement.liuka.model.*;
import com.example.usermanagement.liuka.service.FlowMessageService;
import com.example.usermanagement.liuka.service.FlowPackageService;
import com.example.usermanagement.liuka.service.FlowPackageUserService;
import com.example.usermanagement.liuka.service.FlowUserService;
import com.example.usermanagement.utils.LocaTimeToDateUtil;
import com.example.usermanagement.vo.FlowPackageInfoVO;
import com.example.usermanagement.vo.FlowUserShopVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author yha
 * @ClassName
 * @description:TODO
 * @date 2024年09月10日
 * @version: 1.0.0
 */
@Service
@AllArgsConstructor
public class FlowPackageUserServiceImpl  extends ServiceImpl<FlowPackageUserMapper, FlowPackageUser> implements FlowPackageUserService {

    private final FlowPackageService flowPackageService;
    private final FlowUserService flowUserService;
    private final FlowMessageService flowMessageService;


    @Override
    public void shopPackage(Long packageId, Long flowUserId, String phone) throws ServerErrorException {
        FlowUser flowUser = flowUserService.getOne(Wrappers.lambdaQuery(new FlowUser())
                .eq(FlowUser::getPhone,phone));
        if(Objects.isNull(flowUser)){
            throw new ObjectNotException("购买用户信息不存在");
        }
        if(!flowUser.getId().equals(flowUserId)){
            throw new ParamErrorException("错误用户信息");
        }
        FlowPackage flowPackage = flowPackageService.getById(packageId);
        if(Objects.isNull(flowPackage)){
            throw new ObjectNotException("套餐包信息不存在");
        }
       Date  now = new Date();
        //校验当前时间是否在套餐包有效期内
        Date startTime = LocaTimeToDateUtil.localDateTimeToDate(flowPackage.getStartTime());
        Date endTime = LocaTimeToDateUtil.localDateTimeToDate(flowPackage.getEndTime());
        if(Objects.isNull(startTime)){
            startTime = DateUtil.parseDate("1990-01-01 00:00:00");
        }
        if(Objects.isNull(endTime)){
            endTime =new Date();
        }

         Boolean isDate = DateUtil.isIn(now,startTime,endTime);
        if(!isDate){
            throw new ServerErrorException("套餐包活动已经过期");
        }


        //生效时间
        LocalDateTime shopTime = LocalDateTime.now();
        if(flowPackage.getType().equals("2")){
            shopTime = flowPackage.getCarTime();
        }
        //到期时间
        Date endDate = DateUtil.offsetDay(LocaTimeToDateUtil.localDateTimeToDate(shopTime),flowPackage.getTime().intValue());


        FlowPackageUser flowPackageUser = FlowPackageUser.builder()
                .flowUserId(flowUser.getId())
                .packageId(flowPackage.getId())
                .endTime(LocaTimeToDateUtil.dateToLocalDateTime(endDate))
                .enable(1).shopTime(shopTime)
                .type(flowPackage.getType())
                .flowSize(flowPackage.getFlowSize())
                .readyMode(flowPackage.getReadyMode())
                .sort(flowPackage.getSort())
                .status(1)
                .build();

        save(flowPackageUser);
    }

    @Override
    public void stopFlowPackageUser(Long flowPackageUserId) throws ObjectNotException {
        FlowPackageUser flowPackageUser = getById(flowPackageUserId);
        if(Objects.isNull(flowPackageUser)){
            throw new ObjectNotException("订购信息不存在");
        }
        FlowUser flowUser = flowUserService.getById(flowPackageUser.getFlowUserId());
        FlowPackage flowPackage = flowPackageService.getById(flowPackageUser.getPackageId());
        String message = "[用户]手机号为:"+flowUser.getPhone()+",流量已经用完，即将关闭套餐["+flowPackage.getPackageName()+"]流量包!";
        FlowMessage flowMessage = new FlowMessage();
        flowMessage.setMessage(message);
        flowMessage.setPhone(flowUser.getPhone());
        flowMessageService.save(flowMessage);
        flowPackageUser.setStatus(0);
        updateById(flowPackageUser);
    }

    @Override
    public IPage<FlowPackageInfoVO> selectByPage(Page page, String name, String packageName, String phone) {
        return baseMapper.selectByPage(page,name,packageName,phone);
    }

    @Override
    public FlowMessageService getMessage() {
        return this.flowMessageService;
    }


}
